﻿using System;

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;

public partial class sysadmin_DL_Project_AddBatch : PowerCreator.LMS.Web.Base.WebBasePage
{
    TOBatch objBatch = new TOBatch();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            //BinderRule();
            int BID = 0; 
            if (Request.QueryString["BID"] != null)
            {
                BID = int.Parse(Request.QueryString["BID"].ToString());
                ViewState["BID"] = BID.ToString();
                BinderForm(BID);
            }
        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }

    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        //lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }


    //private void BinderRule()
    //{
    //    TORule objRule = new TORule();
    //    DataTable dt = new DataTable();
    //    dt = objRule.Query("");


    //    ddl_Rule.DataSource = dt;
    //    ddl_Rule.DataTextField = "RuleName";
    //    ddl_Rule.DataValueField = "Rule_Id";
    //    ddl_Rule.DataBind();
    //    //if (cid != 0)
    //    //    ddl_City.SelectedValue = cid.ToString();
    //}




    private void BinderForm(int id)
    {
        VOTOBatch vo = new VOTOBatch();
        vo = objBatch.Load(id);
        txt_Batch.Text = vo.Batch;
        txt_StartTime.Text = vo.StartDate.ToString();
        txt_EndTime.Text = vo.EndDate.ToString();
        txt_xueshi.Text = vo.Batch_xueshi.ToString().Trim();
        txt_xuefen.Text = vo.Batch_xuefen.ToString().Trim();
        txt_Introduce.Text = vo.Description;
        if (vo.Used)
        {
            radbtn_Yes.Checked = true;
            radbtn_No.Checked = false;
        }
        else
        {
            radbtn_Yes.Checked = false;
            radbtn_No.Checked = true;
        }

        //ddl_Rule.SelectedValue = vo.Rule.ToString();
    }
    private bool CheckPageData()
    {
        string search = string.Format("Batch = '{0}'", txt_Batch.Text.Trim());
        if (ViewState["BID"] != null)
        {
            search += "and Batch_Id<>" + ViewState["BID"].ToString();
        }
        if (objBatch.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOBatch vo = new VOTOBatch();
        if (ViewState["BID"] == null)
        {
            vo.Batch = txt_Batch.Text.Trim();
            vo.StartDate = DateTime.Parse(txt_StartTime.Text.Trim());
            vo.EndDate = DateTime.Parse(txt_EndTime.Text.Trim());
            vo.Batch_xueshi = int.Parse(txt_xueshi.Text.Trim());
            vo.Batch_xuefen = int.Parse(txt_xuefen.Text.Trim());
            vo.Description = txt_Introduce.Text.Trim();
            //vo.Rule = int.Parse(ddl_Rule.SelectedValue);
            if (radbtn_Yes.Checked == true)
                vo.Used = true;
            if (radbtn_No.Checked == true)
                vo.Used = false;
            if (objBatch.Add(vo))
            {
                string url = "ManageBatch.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            vo = objBatch.Load(int.Parse(ViewState["BID"].ToString()));
            vo.StartDate = DateTime.Parse(txt_StartTime.Text.Trim());
            vo.EndDate = DateTime.Parse(txt_EndTime.Text.Trim());
            vo.Batch_xueshi = int.Parse(txt_xueshi.Text.Trim());
            vo.Batch_xuefen = int.Parse(txt_xuefen.Text.Trim());
            vo.Description = txt_Introduce.Text.Trim();
            //vo.Rule = int.Parse(ddl_Rule.SelectedValue);
            if (radbtn_Yes.Checked == true)
                vo.Used = true;
            if (radbtn_No.Checked == true)
                vo.Used = false;
            if (objBatch.Update(vo))
            {
                string url = "ManageBatch.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageBatch.aspx";
        Script.RedirectURL(url);
    }


}
